package com.example.composedemo.widget

import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Checkbox
import androidx.compose.material3.TriStateCheckbox
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.state.ToggleableState
import androidx.compose.ui.tooling.preview.Preview

@Preview
@Composable
fun CheckBoxTest() {

    val (checked1, onCheckChanged1) = remember {
        mutableStateOf(false)
    }
    val (checked2, onCheckChanged2) = remember {
        mutableStateOf(false)
    }
    val parentChecked = remember(checked1, checked2) {
        if (checked1 && checked2) {
            ToggleableState.On
        } else if (!checked1 && !checked2) {
            ToggleableState.Off
        } else {
            ToggleableState.Indeterminate
        }
    }

    val parentClicked = {
        val s = parentChecked != ToggleableState.On
        onCheckChanged1(s)
        onCheckChanged2(s)
    }

    Column {
        TriStateCheckbox(state = parentChecked, onClick = parentClicked)
        Checkbox(checked = checked1, onCheckedChange = onCheckChanged1)
        Checkbox(checked = checked2, onCheckedChange = onCheckChanged2)
    }
}